package com.ymatou.iapi.livebuyer.testcase;

import java.util.Date;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryLiveRoomBean;
import com.ymatou.iapi.livebuyer.service.QueryLiveRoomCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.livevideo.parameter.RecLiveNotifyBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 查询直播间信息 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryLiveRoom {
	private static QueryLiveRoomBean queryliveroomBean;
	private static QueryLiveRoomCall queryliveroomCall;
	private static LiveRoomWapper liveRoomWapper;
	private static Integer liveroomid;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询直播间信息");
	}

	@Before
	public void caseUp() {
		queryliveroomBean = new QueryLiveRoomBean();
		queryliveroomCall = new QueryLiveRoomCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryliveroomCall.getOpurl());
		liveRoomWapper = null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private Integer getCodeRate(Integer codeRate) {
		/**
		 * 码率类型，不填写默认为 0 0 - 标清（700码率 - 640高） 1 - 高清（900码率 - 720高） 2 - 超清（1200码率
		 * - 960高） 3 - 省流（550码率 - 540高）
		 */
		Integer code = 700;
		if (codeRate == null) {
			code = 700;
		} else if (codeRate == 0) {
			code = 700;
		} else if (codeRate == 1) {
			code = 900;
		} else if (codeRate == 2) {
			code = 1200;
		} else if (codeRate == 3) {
			code = 550;
		}
		return code;

	}

	private void verifyDB(QueryLiveRoomBean queryLiveRoomBean, LiveRoomInfo liveRoomInfo) throws Exception {
		LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveRoomInfo.getLiveId());
		Logger.verifyEquals(liveroom.getEndTime().subSequence(0, 19), liveRoomInfo.getEndTime(), "验证EndTime");
		Logger.verifyEquals(liveroom.getStartTime().substring(0, 19), liveRoomInfo.getStartTime(), "验证StartTime");
		Logger.verifyEquals(liveroom.getGroupId(), liveRoomInfo.getGroupId(), "验证GroupId");
		//这个字段不用了 用replay表
//		Logger.verifyEquals(liveroom.getHistoryPlayUrl(), liveRoomInfo.getHistoryPlayUrl(), "验证HistoryPlayUrl");
		Logger.verifyEquals(liveroom.getLiveCover(), liveRoomInfo.getLiveCover(), "验证LiveCover");
		Logger.verifyEquals(liveroom.getId(), liveRoomInfo.getLiveId(), "验证LiveId");
		// 修改status
		if (liveroom.getLiveStatus() == 0) {
			if (liveroom.getStartTime().compareTo(YMTDateUtil.getSysTime()) < 0) {
				Logger.verifyEquals(1, liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			} else {
				Logger.verifyEquals(liveroom.getLiveStatus(), liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			}
		}

		Logger.verifyEquals(liveroom.getLiveTitle(), liveRoomInfo.getLiveTitle(), "验证LiveTitle");
		if (queryLiveRoomBean.getTerminalType() == null) {
			String playurl = DesUtil.decrypt(liveRoomInfo.getPlayUrl(), DesUtil.DES_KEY_STRING, DesUtil.DES_IVPARAMETER);
			// rtmp://12808.liveplay.myqcloud.com/live/12808_SIT1_4028_550
			Logger.debug("playurl：" + playurl);
			String mypurl = "http://" + LiveRoomCallService.BizId + ".liveplay.myqcloud.com/live/" + LiveRoomCallService.BizId + "_" + EnvSetup.getEnv().toUpperCase() + "_"
					+ liveroom.getId() + "_" + getCodeRate(queryliveroomBean.getCodeRate())+".flv";
			Logger.verifyEquals(mypurl, playurl, "验证PlayUrl");
		} else if (queryLiveRoomBean.getTerminalType().equals(0)) {
			String playurl = DesUtil.decrypt(liveRoomInfo.getPlayUrl(), DesUtil.DES_KEY_STRING, DesUtil.DES_IVPARAMETER);
			// rtmp://12808.liveplay.myqcloud.com/live/12808_SIT1_4028_550
			Logger.debug("playurl" + playurl);
			String mypurl = "http://" + LiveRoomCallService.BizId + ".liveplay.myqcloud.com/live/" + LiveRoomCallService.BizId + "_" + EnvSetup.getEnv().toUpperCase() + "_"
					+ liveroom.getId() + "_" + getCodeRate(queryliveroomBean.getCodeRate())+".flv";
			Logger.verifyEquals(mypurl, playurl, "验证PlayUrl");

		} else if (queryLiveRoomBean.getTerminalType().equals(1)) {
			String playurl = liveRoomInfo.getPlayUrl();
			// rtmp://12808.liveplay.myqcloud.com/live/12808_SIT1_4028_550
			Logger.debug("playurl" + playurl);
			String mypurl = "http://" + LiveRoomCallService.BizId + ".liveplay.myqcloud.com/live/" + LiveRoomCallService.BizId + "_" + EnvSetup.getEnv().toUpperCase() + "_"
					+ liveroom.getId() + "_" + getCodeRate(queryliveroomBean.getCodeRate()) + ".m3u8";
			Logger.verifyEquals(mypurl, playurl, "验证PlayUrl");

		} else if (queryLiveRoomBean.getTerminalType().equals(2)) {
			String playurl = liveRoomInfo.getPlayUrl();
			// rtmp://12808.liveplay.myqcloud.com/live/12808_SIT1_4028_550
			Logger.debug("playurl" + playurl);
			String mypurl = "http://" + LiveRoomCallService.BizId + ".liveplay.myqcloud.com/live/" + LiveRoomCallService.BizId + "_" + EnvSetup.getEnv().toUpperCase() + "_"
					+ liveroom.getId() + "_" + getCodeRate(queryliveroomBean.getCodeRate()) + ".m3u8";
			Logger.verifyEquals(mypurl, playurl, "验证PlayUrl");

		}

		if (liveRoomInfo.getPreVideo() != null) {
			Logger.verifyEquals(liveroom.getPreVideoHeight(), liveRoomInfo.getPreVideo().getPreVideoHeight(), "验证getPreVideoHeight");
			Logger.verifyEquals(liveroom.getPreVideoLength(), liveRoomInfo.getPreVideo().getPreVideoLength(), "验证getPreVideoLength");
			Logger.verifyEquals(liveroom.getPreVideoUrl(), liveRoomInfo.getPreVideo().getPreVideoUrl(), "验证getPreVideoUrl");
			Logger.verifyEquals(liveroom.getPreVideoWidth(), liveRoomInfo.getPreVideo().getPreVideoWidth(), "验证getPreVideoWidth");
		}
		Logger.verifyEquals(liveroom.getUserId(), liveRoomInfo.getUserId(), "验证UserId");
		Logger.verifyEquals(liveroom.getLiveDesc(), liveRoomInfo.getLiveDesc(), "验证LiveDesc");
		Logger.verifyEquals(liveroom.getPutawayCount(), liveRoomInfo.getPutawayCount(), "验证PutawayCount");
		if (liveRoomInfo.getAction() != null) {
			Logger.verifyEquals(liveroom.getAction(), liveRoomInfo.getAction(), "验证Action");
		}
		int preViewNum = liveRoomInfo.getPreViewNum();
		int viewnum = liveRoomInfo.getViewNum();
		Map mstat = liveRoomWapper.selectLiveRoomStatById(liveroomid);
		if (mstat != null) {
			Logger.verifyEquals(mstat.get("ViewNum"), liveRoomInfo.getViewNum(), "验证ViewNum");
			Logger.verifyEquals(mstat.get("PreViewNum"), liveRoomInfo.getPreViewNum(), "验证PreViewNum");

		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_001() {
		Logger.start(true, "查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryLiveRoom_002() {
		Logger.start(true, "不输入liveid失败");
		try {
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("100", queryliveroomCall.getString("code"), "判断code");
			Logger.verifyIsContains("错误的请求参数", queryliveroomCall.getString("message"), "判断message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_003() {
		Logger.start(true, "标题含有特殊字符查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				initcreatebean.getLiveDto().setLiveTitle("测试特殊字符标题22:;;;;😑😊😳");
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_004() {
		Logger.start(true, "查询可回播直播间信息成功");
		try {
			LiveRoom livedto = null;
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveroomid = livedto.getId();
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			LiveRoomInfo info = queryliveroomCall.getLiveRoomInfo();
			Logger.verifyEquals(false, info.getIsPossibleReplay(), "验证isPossibleReplay");
			verifyDB(queryliveroomBean, info);
			LiveRoomCallService.CloseSellerLive(liveroomid, livedto.getUserId(), 8);
			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(liveroomid, 100);
			LiveRoomCallService.LiveNotify(recivenotifyBean);
			Thread.sleep(2000);
			queryliveroomCall.callService();
			info = queryliveroomCall.getLiveRoomInfo();
			Logger.verifyEquals(true, info.getIsPossibleReplay(), "验证isPossibleReplay");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_005() {
		Logger.start(true, "TerminalType为0:app查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomBean.setTerminalType(0);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_006() {
		Logger.start(true, "TerminalType为1:wap查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomBean.setTerminalType(1);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_007() {
		Logger.start(true, "TerminalType为2:pc查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomBean.setTerminalType(2);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_008() {
		Logger.start(true, "CodeRate为0查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomBean.setTerminalType(0);
			queryliveroomBean.setCodeRate(0);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_009() {
		Logger.start(true, "CodeRate为1查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomBean.setTerminalType(1);
			queryliveroomBean.setCodeRate(1);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_010() {
		Logger.start(true, "CodeRate为2查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomBean.setTerminalType(1);
			queryliveroomBean.setCodeRate(2);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_011() {
		Logger.start(true, "CodeRate为3查询直播间信息成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomBean.setTerminalType(1);
			queryliveroomBean.setCodeRate(3);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));

			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoom_012() {
		Logger.start(true, "查询想看数观看数直播间信息成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			liveroomid = livedto.getId();
			LiveRoomCallService.enterGroup(liveroomid, 1);
			LiveRoomCallService.SellerLiveOnline(liveroomid, livedto.getUserId());
			LiveRoomCallService.enterGroup(liveroomid, 2);
			Thread.sleep(1000);
			queryliveroomBean.setLiveId(liveroomid);
			queryliveroomCall.setData(queryliveroomBean);
			queryliveroomCall.callService();
			Logger.verifyEquals("200", queryliveroomCall.getString("code"), "判断code是否为200");
			Logger.debug(FormatUtil.beanToGSONString(queryliveroomCall.getLiveRoomInfo()));
			verifyDB(queryliveroomBean, queryliveroomCall.getLiveRoomInfo());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}